﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.PublicOfficialSelectionService;

namespace CommunitySystem
{
    public partial class ChengJiWindow : ChildWindow
    {
        private PublicOfficialSelectionServiceClient Client = new PublicOfficialSelectionServiceClient();
        private PublicOfficialRecordModel model = new PublicOfficialRecordModel();
        public bool IsNew = false;//true则为编辑笔试成绩，否则为编辑面试成绩
        public int ID;
        public ChengJiWindow(int PublicOfficialRecordID, bool isnew)
        {
            InitializeComponent();
            ID = PublicOfficialRecordID;
            IsNew = isnew;
            if(IsNew == true)
            {
                bishi.IsEnabled = true;
                mianshi.IsEnabled = false;
            }
            else
            {
                bishi.IsEnabled = false;
                mianshi.IsEnabled = true;
            }
            Load();
        }
        private string pattern = @"^[0-9]+([.]{1}[0-9]+){0,1}$";
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                double.Parse(((TextBox)sender).Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                ((TextBox)sender).Text = "";
            }
            //Match m = Regex.Match(((TextBox)sender).Text, pattern);   // 匹配正则表达式
            //if (!string.IsNullOrEmpty(((TextBox)sender).Text) && ((TextBox)sender).Text != "0")
            //if (!m.Success)   // 输入的不是数字
            //{
            //    SilverlightMessageBoxLibrary.Message.ErrorMessage("只能输入数字！");
            //    ((TextBox)sender).Text = "";
            //    //((TextBox)sender).Text = param1;   // textBox内容不变

            //    // 将光标定位到文本框的最后
            //    //((TextBox)sender).SelectionStart = ((TextBox)sender).Text.Length;
            //}

        }

        public void Load()
        {
            Client.GetPublicOfficialRecordbyEmpIDCompleted += new EventHandler<PublicOfficialSelectionService.GetPublicOfficialRecordbyEmpIDCompletedEventArgs>(Client1_GetPublicOfficialRecordbyEmpIDCompleted);
            Client.GetPublicOfficialRecordbyEmpIDAsync(ID,false,0,"0",0);
        }

        void Client1_GetPublicOfficialRecordbyEmpIDCompleted(object sender, PublicOfficialSelectionService.GetPublicOfficialRecordbyEmpIDCompletedEventArgs e)
        {
            if(e.Result != null && e.Result.Count > 0)
            this.DataContext = model = e.Result[0];

        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (Convert.ToDecimal(bishi.Text) > 100 || Convert.ToDecimal(mianshi.Text) > 100)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("输入的成绩有误，请重新输入！");
                return;
            }
            Client.UpdatePublicOfficialRecordModelFenShuCompleted += new EventHandler<UpdatePublicOfficialRecordModelFenShuCompletedEventArgs>(Client_UpdatePublicOfficialRecordModelFenShuCompleted);
            Client.UpdatePublicOfficialRecordModelFenShuAsync(model);
        }

        void Client_UpdatePublicOfficialRecordModelFenShuCompleted(object sender, UpdatePublicOfficialRecordModelFenShuCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功!");
            this.DialogResult = true;
        }
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void Text_LostFocus(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox)sender).Text))
            if(model.FaBuType == "竞争性选拔")
            {
                if (!string.IsNullOrEmpty(bishi.Text))
                {
                    total.Text =
                        Math.Round(
                            (Math.Round(Convert.ToDouble(bishi.Text) * 0.4, 2) +
                             Math.Round(Convert.ToDouble(mianshi.Text) * 0.6, 2)), 2).ToString();
                    model.TotalScore = Convert.ToDecimal(total.Text);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(bishi.Text))
                {
                    total.Text = mianshi.Text;
                    model.TotalScore = Convert.ToDecimal(total.Text);
                }
            }
        }

    }
}
